<template>
  <!-- 导入字段 -->
  <el-dialog
    title="导入字段"
    v-model="visible"
    width="800px"
    top="5vh"
    append-to-body
  >
    <el-form :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="字段编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入字段编码"
          clearable
          style="width: 180px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字段名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入字段名称"
          clearable
          style="width: 180px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-table
        @row-click="clickRow"
        ref="fieldRef"
        :data="fieldList"
        @selection-change="handleSelectionChange"
        height="260px"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="Id" align="center" prop="id" />
        <el-table-column label="编码" align="left" prop="code" />
        <el-table-column label="名称" align="left" prop="name" />
        <el-table-column label="字段类型" align="left" prop="fieldType">
          <template #default="scope">
            <dict-tag
              :options="barcode_filed_type"
              :value="scope.row.fieldType"
            />
          </template>
        </el-table-column>
        <el-table-column label="字段名" align="left" prop="fieldName" />
        <el-table-column label="小数位" align="left" prop="decimalPlaces" />
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleImportField">确 定</el-button>
        <el-button @click="visible = false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { listField } from "@/api/barcode/field";

const total = ref(0);
const visible = ref(false);
const fields = ref([]);
const fieldList = ref([]);
const { proxy } = getCurrentInstance();
const { barcode_filed_type } = proxy.useDict('barcode_filed_type');

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  code: undefined,
  name: undefined,
});

const emit = defineEmits(["ok"]);

/** 查询参数列表 */
function show() {
  getList();
  visible.value = true;
}

function hide(){
  visible.value = false;
}

/** 单击选择行 */
function clickRow(row) {
  proxy.$refs.fieldRef.toggleRowSelection(row);
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  fields.value = selection.map((item) => item.id);
}

/** 查询表数据 */
function getList() {
  listField(queryParams).then((res) => {
    fieldList.value = res.rows;
    total.value = res.total;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 导入按钮操作 */
function handleImportField() {
  if (proxy.$refs.fieldRef.getSelectionRows().length < 1) {
    proxy.$modal.msgError("请至少选择一条数据!");
    return true;
  }
  emit("ok", proxy.$refs.fieldRef.getSelectionRows());
}

defineExpose({
  show,
  hide,
});
</script>
